package com.qf.listeners;

import com.qf.annotations.RequestMapping;
import com.qf.utils.RequestMappingUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

@WebListener
public class LoadRequestMappinglistener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        Package aPackage = LoadRequestMappinglistener.class.getPackage();
        String dirPath = aPackage.toString().replace("package ", "").replaceAll("\\.", Matcher.quoteReplacement(File.separator));
        System.out.println(dirPath);

        String startPath = dirPath.substring(0, dirPath.indexOf(File.separator));

        URL url = LoadRequestMappinglistener.class.getResource("/" + startPath);
        String startRealPath = url.toString().replace("file:", "");
        File file = new File(startRealPath);
        System.out.println(file);
        List<String> allClassNames = processFile(file, startPath);

        allClassNames.forEach(className -> {
            try {
                Class clazz = Class.forName(className);
                String pre = "";
                RequestMapping annotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
                if (annotation != null) {
                    pre=annotation.value();
                }
                Method[] methods = clazz.getMethods();
                for (Method method : methods) {
                    RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                    if (requestMapping != null) {
                        String path = requestMapping.value();
                        path = pre + path;
                        RequestMappingUtils.addMethod(path, className + "." + method.getName());
                    }

                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        });
    }


    @Override
    public void contextDestroyed(ServletContextEvent sce) {
//        ServletContextListener.super.contextDestroyed(sce);
    }

    public List<String> processFile(File file, String startPath) {
        List<String> allClassNames = new ArrayList<>();
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                allClassNames.addAll(processFile(file1, startPath));
            } else {
                String path = file1.getPath();
                String classPath = path.substring(path.indexOf(startPath), path.lastIndexOf("."));
                String refrencePath = classPath.replaceAll(Matcher.quoteReplacement(File.separator), ".");
                allClassNames.add(refrencePath);
            }
        }
        return allClassNames;
    }
}
